package com.sun.weekend.view.refresh;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Scroller;
import android.widget.TextView;

import com.sun.weekend.R;

/**
 * @author Administrator
 */

public abstract class RefreshLayoutBase <T extends View> extends ViewGroup{
    private Context context;
    protected LayoutInflater inflater;
    protected Scroller scroller;
    protected ImageView head_arrowImageView;
    protected View headerView;
    protected View footerView;
    protected int yOffset;
    protected T contentView;
    protected int initScrollY = 0;
    protected int lastY = 0;
    protected static final int STATUS_IDLE = 0;
    protected static final int STATUS_PULL_TO_REFRESH = 1;
    protected static final int STATUS_LOAD_MORE = 2;
    protected static final int STATUS_REFRESHING = 3;
    protected static final int STATUS_LOADING = 4;
    protected int currentStatu = STATUS_IDLE;
    private int screenHeight;
    private int headHeight;
    protected  OnRefreshListener onRefreshListener;
    protected OnLoadListener onLoadListener;
    private TextView head_lastUpdatedTextView;
    private TextView head_tipsTextView;
    private TextView listview_foot_more;

    public RefreshLayoutBase(Context context) {
        this(context,null);
    }

    public RefreshLayoutBase(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RefreshLayoutBase(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        scroller = new Scroller(context);
        screenHeight = getResources().getDisplayMetrics().heightPixels;
        headHeight = screenHeight/4;
        inflater = LayoutInflater.from(context);
        initLayout(context);
    }

    private final void initLayout(Context context) {
        setUpHeadView(context);
        setUpContentView(context);
        setUpFootView(context);
    }

    private void setUpHeadView(Context context) {
        headerView = inflater.inflate(R.layout.pull_to_refresh_head,null);
        head_arrowImageView = (ImageView) headerView.findViewById(R.id.head_arrowImageView);
        head_tipsTextView = (TextView) headerView.findViewById(R.id.head_tipsTextView);
        head_lastUpdatedTextView = (TextView) headerView.findViewById(R.id.head_lastUpdatedTextView);
        headerView.setPadding(0,headHeight-100,0,0);
        addView(headerView);
    }

    protected abstract void setUpContentView(Context context);

    protected abstract boolean isTop();
    protected abstract boolean isBottom();
    private void setUpFootView(Context context) {
        footerView = inflater.inflate(R.layout.listview_footer,null);
        listview_foot_more = (TextView) footerView.findViewById(R.id.listview_foot_more);
        addView(footerView);
    }

    public interface OnRefreshListener{
        void onRefresh();
    }


    public interface OnLoadListener{
        void onLoad();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int chidecount = getChildCount();
        int finalheight = 0;
        for (int i = 0; i < chidecount; i++) {
            View child = getChildAt(i);
            measureChild(child,widthMeasureSpec,heightMeasureSpec);
            finalheight += child.getMeasuredHeight();
        }
        setMeasuredDimension(width,finalheight);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int left =getPaddingLeft();
        int top = getPaddingTop();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(left,top,child.getMeasuredWidth(),child.getMeasuredHeight()+top);
            top+=child.getMeasuredHeight();
            initScrollY = headerView.getMeasuredHeight()+getPaddingTop();
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                int currentY = (int) event.getRawY();
                yOffset = currentY - lastY;
                if (currentStatu != STATUS_LOADING) {

                }
                lastY = currentY;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            return false;
        }
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                lastY = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                yOffset = (int) (ev.getRawY() - lastY);
                if (isTop() && yOffset > 0) {
                    return true;
                }
                break;
        }
        return false;
    }
}
